package com.visitor.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.visitor.bean.Visitor;
import com.visitor.mapper.VisitorMapper;
import com.visitor.web.vm.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VisitorService {
	@Resource
	private VisitorMapper VisitorMapper;

	@Transactional
	public Response<String> saveOrUpdate(Visitor user) {
		user.setAppointmentTime(user.getAppointmentTime().replace("点",""));
		user.setShows("0");
		VisitorMapper.insert(user);
		return Response.ok();
	}
	
	public Visitor findUserById(Long id) {
		Visitor user = VisitorMapper.selectByPrimaryKey(id);
		return user;
	}

	public List<Visitor> findVisitorList(String phone) {
		Visitor visitor=new Visitor();
		visitor.setPhone(phone);
		return  VisitorMapper.selectAll(visitor);
	}

	public int deleteUserById(Long id) {
		return VisitorMapper.deleteByPrimaryKey(id);
	}
	public int updateShow() {
		return VisitorMapper.updateShow();
	}
	public int getNewVisitor() {
		Visitor visitor=new Visitor();
		visitor.setShows("0");
		return  VisitorMapper.selectAll(visitor).size();
	}
	
	public PageInfo<Visitor> getAllByPage(int pageNum,int pageSize){
		PageHelper.startPage(pageNum, pageSize, true);
		List<Visitor> list= VisitorMapper.selectAll(null);
		if(list==null||list.isEmpty()) {
			list = new ArrayList<>();
		}
		return new PageInfo<>(list);
	}
}
